﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Xml;
using System.Xml.Xsl;
using System.Text;

namespace ReportService.Common
{
    public class ReportUtils
    {
        public static string[] MonthShortText = { "-free-", "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC" };
        public static string[] MonthLongText = { "-free-", "january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december" };

        public static string GetMonth(int month)
        {
            switch (month)
            {
                case 1: return "JAN";
                case 2: return "FEB";
                case 3: return "MAR";
                case 4: return "APR";
                case 5: return "MAY";
                case 6: return "JUN";
                case 7: return "JUL";
                case 8: return "AUG";
                case 9: return "SEP";
                case 10: return "OCT";
                case 11: return "NOV";
                case 12: return "DEC";
                default: return string.Empty;
            }
        }

        public static string GetMonthOfSummaryReport(int month)
        {
            switch (month)
            {
                case 1: return "january";
                case 2: return "february";
                case 3: return "march";
                case 4: return "april";
                case 5: return "may";
                case 6: return "june";
                case 7: return "july";
                case 8: return "august";
                case 9: return "september";
                case 10: return "october";
                case 11: return "november";
                case 12: return "december";
                default: return string.Empty;
            }
        }

        public static void SendExportFile(string xmlString, string pathTempXLS, string fileName)
        {
            ResponseExcelFile(CreateXLSFile(xmlString, pathTempXLS), fileName);
        }

        private static string CreateXLSFile(string xmlString, string pathTempXLS)
        {
            string localPath = HttpContext.Current.Request.PhysicalApplicationPath;


            string excelpath = localPath + "\\Content\\temp" + DateTime.Now.ToString("yyyyMMddmm") + ".xls";
            FileStream fs = new FileStream(excelpath, FileMode.Create);
            XmlTextWriter tw = new XmlTextWriter(fs, Encoding.Unicode);
            XslCompiledTransform xslt = new XslCompiledTransform(true);
            XmlDataDocument xmlData = new XmlDataDocument();

            try
            {
                xslt.Load(pathTempXLS);

                xmlData.LoadXml(xmlString);

                xslt.Transform(xmlData, null, tw);
            }
            catch
            { }
            finally
            {
                tw.Close();
                fs.Close();
            }

            return excelpath;
        }

        private static void ResponseExcelFile(string strExcelFile, string reportFile)
        {
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + reportFile);
            HttpContext.Current.Response.Charset = "";
            HttpContext.Current.Response.ContentType = "application/vnd.xls";
            HttpContext.Current.Response.WriteFile(strExcelFile);
            HttpContext.Current.Response.End();
        }
    }
}